package com.jjyd.crlp.controller;

import com.jjyd.crlp.interceptor.LoginInterceptor;
import com.jjyd.crlp.interceptor.dto.JWTDto;
import com.jjyd.crlp.model.base.ResultListModel;
import com.jjyd.crlp.model.base.ResultModel;
import com.jjyd.crlp.model.dto.word.SearchDto;
import com.jjyd.crlp.model.dto.word.WordDto;
import com.jjyd.crlp.services.WordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/word")
@Api(description = "主页面接口")
public class WordController {

    @Autowired
    private WordService wordService;

    @PostMapping("")
    @ApiOperation(value = "测试专用")
    public ResultModel<String> test(JWTDto jwtDto) {
        Integer userId = LoginInterceptor.getUserId();
        return new ResultModel<>("aaa");
    }


    /**
     * 搜索单词
     * @param searchDto 原始单词
     * @return 返回搜索到的单词的内容
     */
    @GetMapping("/search")
    @ApiOperation(value = "搜索单词")
    public ResultListModel<WordDto, Object> search(SearchDto searchDto) {
        //无搜索内容
        if (searchDto.getSearch() == null || "".equals(searchDto.getSearch())) {
            return new ResultListModel<WordDto, Object>(null);
        }

        List<WordDto> search = wordService.search(searchDto);

        return new ResultListModel<WordDto, Object>(search);
    }


    /**
     * 用户收藏单词功能接口
     * @param wordId 单词id
     * @return 返回收藏记录id
     */
    @GetMapping("/collect")
    @ApiOperation(value = "收藏单词")
    public ResultModel<Integer> collect(JWTDto jwtDto,
            @ApiParam(value = "单词的id", required = true) @RequestParam("wordId") Integer wordId) {
        //获取用户id
        Integer userId = LoginInterceptor.getUserId();
        Integer collect = wordService.collect(wordId, userId);
        return new ResultModel<>(collect);
    }



    /**
     * 用户取消收藏单词功能接口
     * @param wordId 取消的单词id
     * @return 返回收藏记录id
     */
    @GetMapping("/cancelCollect")
    @ApiOperation(value = "取消收藏单词")
    public ResultModel<Integer> cancelCollect(JWTDto jwtDto,
            @ApiParam(value = "单词的id", required = true) @RequestParam("wordId") Integer wordId) {
        Integer userId = LoginInterceptor.getUserId();
        Integer cancelCollect = wordService.cancelCollect(wordId, userId);
        return new ResultModel<>(cancelCollect);
    }


    /**
     * 查找个人所有收藏的单词
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查找个人所有收藏的单词")
    public ResultListModel<WordDto, Object> findAll(JWTDto jwtDto) {
        Integer userId = LoginInterceptor.getUserId();
        List<WordDto> all = wordService.findAll(userId);
        return new ResultListModel<WordDto, Object>(all);
    }


    /**
     * 随机给前台返回单词
     * @return 返回随机获得的单词
     */
    @GetMapping("/random")
    @ApiOperation(value = "随机返回给前台一定数目的单词")
    public ResultListModel<WordDto, Object> random() {
        List<WordDto> random = wordService.random();
        return new ResultListModel<WordDto, Object>(random);
    }


}
